package com.ruoyi.project.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.DtFinalResult;
import com.sun.istack.NotNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.DtRecharge;
import com.ruoyi.project.system.service.IDtRechargeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 充值记录Controller
 */
@RestController
@RequestMapping("/system/recharge")
public class DtRechargeController extends BaseController {
    @Autowired
    private IDtRechargeService dtRechargeService;

    /**
     * 查询充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:recharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(DtRecharge dtRecharge) {
        if (!SecurityUtils.isAdmin()) {
            dtRecharge.setUserId(SecurityUtils.getUserId());
        }
        startPage();
        List<DtRecharge> list = dtRechargeService.selectDtRechargeList(dtRecharge);
        return getDataTable(list);
    }

    /**
     * 获取充值记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:recharge:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        DtRecharge result = dtRechargeService.selectDtRechargeById(id);
        Long userId = SecurityUtils.getUserId();
        if (result.getUserId().longValue() == userId.longValue()) {
            return success(result);
        } else {
            return warn("非法请求");
        }
    }

    /**
     * 新增充值记录
     */
    @PreAuthorize("@ss.hasPermi('system:recharge:add')")
    @Log(title = "充值记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DtRecharge dtRecharge) {
        return toAjax(dtRechargeService.insertDtRecharge(dtRecharge));
    }

    /**
     * 修改充值记录
     */
    @PreAuthorize("@ss.hasPermi('system:recharge:edit')")
    @Log(title = "充值记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DtRecharge dtRecharge) {
        return toAjax(dtRechargeService.updateDtRecharge(dtRecharge));
    }


    /**
     * 佣金分发
     */
    @PreAuthorize("@ss.hasPermi('system:recharge:edit')")
    @Log(title = "佣金分发", businessType = BusinessType.UPDATE)
    @GetMapping("/grant")
    public AjaxResult grant(@NotNull Integer id) {
        DtRecharge recharge = dtRechargeService.selectDtRechargeById(id);
        if (recharge == null) {
            return warn("订单不存在");
        } else if (!"CREATE_NO_GRANT".equals(recharge.getStatus())) {
            return warn("订单状态不支持分发");
        }
        return toAjax(dtRechargeService.grant(recharge));
    }

    /**
     * 删除充值记录
     */
    @PreAuthorize("@ss.hasPermi('system:recharge:remove')")
    @Log(title = "充值记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(dtRechargeService.deleteDtRechargeByIds(ids));
    }
}
